﻿using System;
using System.Collections.Generic;
using System.Reflection;
namespace Validate.Net.Tools
{
    /// <summary>
    /// 缓存列描述信息
    /// </summary>
    public static class CacheColumnDescribe
    {
        public static Dictionary<PropertyInfo, string> columnDescribeCache = new Dictionary<PropertyInfo, string>();

        /// <summary>
        /// 缓存字段上中文名称
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static string GetColumnDescribe(PropertyInfo property)
        {
            if (!columnDescribeCache.ContainsKey(property))
            {
                string columnName = string.Empty;

                //检测该属性是否包含[DescribeAttribute]特性
                if (property.IsDefined(typeof(DescribeAttribute), true))
                {
                    DescribeAttribute attribute = (DescribeAttribute)Attribute.GetCustomAttribute(property, typeof(DescribeAttribute));
                    columnName = attribute.describe;
                }
                else
                    columnName = property.Name;
                columnDescribeCache.Add(property, columnName);
                return columnName;
            }
            else
            {
                return columnDescribeCache[property];
            }
        }
    }

    /// <summary>
    /// 缓存实体属性上的特性集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static class CachePropertyAttribute<T> where T :new ()
    {
        public static Dictionary<PropertyInfo, object[]> PropertiesCache = new Dictionary<PropertyInfo, object[]>();
        static CachePropertyAttribute()
        {
            Type type = typeof(T);

            foreach (PropertyInfo prop in type.GetProperties())
            {
                PropertiesCache.Add(prop, prop.GetCustomAttributes(typeof(AbstractValidateAttribute), true));
            }
        }
    }
}
